<?php
class shift extends baseobject
{
	public function __construct($params) 
	{
		parent::init($params);
		
		if(!isset($params['shiftno'])) {
			$h = date("H");
			if($h>=16&&$h<=23)
			{
				$this->shiftno=3;
			}
			elseif($h>=8&&$h<=15)
			{
				$this->shiftno=2;
			}
			$this->shiftno=1;
		}
		//echo "SHIFTNO: $h ". $this->shiftno;		
	}
	
	public function getShiftDuration_standard()
	{
		 
		$sql = "select shift_start, shift_end from shifts where shift_id=".$this->shiftno;
		$res = mysql_query($sql);
		list($this->hrStart,$this->hrEnd) = mysql_fetch_row($res);
		$this->shiftStart = $this->date . ' ' . $this->hrStart . ':01:00';
		$this->shiftEnd = date('Y-m-d H:i:s', strtotime($this->date . ' ' . $this->hrEnd . ':01:00'));
	}

	public function getShiftDuration()
	{
		$sql = "select shift_start, shift_end from shifts where shift_id=".$this->shiftno;
		$res = mysql_query($sql) ;
		list($this->hrStart,$this->hrEnd) = mysql_fetch_row($res);
		list($year,$month,$day) = explode('-',$this->date); 
		$startmax = date('Y-m-d H:i:s', strtotime($this->date . ' ' . $this->hrStart . ':05:00'));
		$sql = "select `datetime` from `shift-transactions`  
				where shift='end'
				and year(`datetime`)='$year'
				and month(`datetime`)='$month'
				and day(`datetime`)='$day'
				and `datetime` < '$startmax' 
				order by `datetime` desc
				limit 1";
		$res = mysql_query($sql) or die($sql);


		list($this->shiftStart)=mysql_fetch_row($res);
		if(is_null($this->shiftStart)) {
			$this->shiftStart=date('Y-m-d H:i:s', strtotime($this->date . ' ' . $this->hrStart . ':00:01'));
		}


		$endmax =  date('Y-m-d H:i:s', strtotime($this->date . ' ' . $this->hrEnd . ':05:00'));
		$sql = "select `datetime` from `shift-transactions`  
				where shift='end' 
				and year(`datetime`)='$year'
				and month(`datetime`)='$month'
				and day(`datetime`)='$day'
				and `datetime` < '$endmax'
				order by `datetime` desc
				limit 1";
		$res = mysql_query($sql) or die($sql);
		
		list($this->shiftEnd)=mysql_fetch_row($res);
		if(is_null($this->shiftEnd) || $this->shiftEnd == $this->shiftStart) {
			$this->shiftEnd=date('Y-m-d H:i:s', strtotime($this->date . ' ' . $this->hrEnd . ':00:00'));
		}

	}
}